package com.feihao.zhou.test1;

import com.feihao.zhou.bean.*;
import com.feihao.zhou.mapper.*;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test01 {
    /*-------------------------------单表注解CRUD:::------------------------------------------------------*/


    @Test
    public void selectAllStudent() {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = build.openSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            //分页助手：设置第一页显示3条
            PageHelper.startPage(3, 2);

            List<Student> list = mapper.selectAllStudent();


            for (Student student : list) {
                System.out.println(student);
            }

            is.close();
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4() {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = build.openSession();
            UseAnnotationToCRUDForSingleTable mapper = sqlSession.getMapper(UseAnnotationToCRUDForSingleTable.class);

//            PageHelper.startPage(1, 2);
            List<Student> list = mapper.selectStudentAll();

            for (Student student : list) {
                System.out.println(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test5() {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = build.openSession(true);
            UseAnnotationToCRUDForSingleTable mapper = sqlSession.getMapper(UseAnnotationToCRUDForSingleTable.class);
            Student student = new Student(6, "小四四", 28, 1);
            Integer integer = mapper.insertStudent(student);

            System.out.println(integer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    //测试未返回主键自增的返回值！！    不会返回主键自增的值
    public void test6() {
        SqlSession sqlSession = null;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            UseAnnotationToCRUDForSingleTable mapper = sqlSession.getMapper(UseAnnotationToCRUDForSingleTable.class);
            Student student = new Student(null, "小八八", 28, 2);

            System.out.println("添加之前的id:" + student.getId());
            Integer integer = mapper.insertStudent2(student);
            sqlSession.commit();
            System.out.println("添加之后的id:" + student.getId());
            System.out.println(integer);
        } catch (IOException e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
    }

    @Test
    //增加@Options(useGeneratedKeys = true,keyColumn = "id" , keyProperty = "id")
    //测试返回主键自增的返回值！！    返回主键自增的值
    public void test7() {
        SqlSession sqlSession = null;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            UseAnnotationToCRUDForSingleTable mapper = sqlSession.getMapper(UseAnnotationToCRUDForSingleTable.class);
            Student student = new Student(null, "小九九", 29, 1);

            System.out.println("添加之前的id:" + student.getId());
            Integer integer = mapper.insertStudent2(student);
            sqlSession.commit();
            System.out.println("添加之后的id:" + student.getId());
            System.out.println(integer);
        } catch (IOException e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
    }

    @Test
    //增加根据具体值修改学生：


    public void test8() {
        SqlSession sqlSession = null;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            UseAnnotationToCRUDForSingleTable mapper = sqlSession.getMapper(UseAnnotationToCRUDForSingleTable.class);
            Integer integer = mapper.updateStudent("张三三", 233, 1);
            sqlSession.commit();

            System.out.println(integer);
        } catch (IOException e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
    }

    //删除:
    @Test
    public void deleteStudent() {
        SqlSession sqlSession = null;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            UseAnnotationToCRUDForSingleTable mapper = sqlSession.getMapper(UseAnnotationToCRUDForSingleTable.class);
            Integer integer = mapper.deleteStudent(2);
            sqlSession.commit();

            System.out.println(integer);
        } catch (IOException e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
    }


    //根据id查询学生
    @Test
    public void selectStudentById() {
        SqlSession sqlSession = null;
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession();
            UseAnnotationToCRUDForSingleTable mapper = sqlSession.getMapper(UseAnnotationToCRUDForSingleTable.class);
            Student student = mapper.selectStudentById(2);
            System.out.println(student);
        } catch (IOException e) {
            e.printStackTrace();
            sqlSession.rollback();
        }
    }
    /*-------------------------------单表注解CRUD:  end------------------------------------------------------*/




    /*-------------------------------多表注解r:查询：：:::------------------------------------------------------*/

    //一对一查询所有
    @Test
    public void test001() {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            OneToOneMapper mapper = sqlSession.getMapper(OneToOneMapper.class);
            List<Card> cards = mapper.selectALLCard();
            cards.forEach((s) -> {
                System.out.println(s.getId() + "," + s.getNumber());
                System.out.println(s.getPerson());
            });

            /*for (Card card : cards) {
                System.out.println(card);
                Person person = card.getPerson();
                System.out.println(person);
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //一对多查询所有：

    @Test
    public void test002() {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = build.openSession();

            OneToMany mapper = sqlSession.getMapper(OneToMany.class);
            List<Classes> classes = mapper.selectAllClasses();
            classes.forEach((s) -> {
                System.out.println(s.getId() + "--" + s.getName() + "/t");
                List<Student> students = s.getStudents();
                System.out.println(students);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //多对多查询所有：
    @Test
    public void test03() {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = build.openSession();

            ManyToMany mapper = sqlSession.getMapper(ManyToMany.class);
            List<Student_Course> list = mapper.selectAllStudent();
            for (Student_Course student : list) {
                System.out.println(student.getId() + student.getName() + student.getAge());
                List<Course> course = student.getCourse();
                for (Course course1 : course) {
                    System.out.println(course1);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
